package com.sumfun.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumfun.blog.annotation.RequireLogin;
import com.sumfun.blog.common.BaseResponse;
import com.sumfun.blog.common.ErrorCode;
import com.sumfun.blog.common.PageRequest;
import com.sumfun.blog.common.ResultUtils;
import com.sumfun.blog.exception.BusinessException;
import com.sumfun.blog.model.domain.Category;
import com.sumfun.blog.model.request.CategoryAddRequest;
import com.sumfun.blog.service.CategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 分类Controller
 *
 * @author sumfun
 * @date 2024-04-21
 */
@Api(value = "分类信息Controller", tags = {"分类信息"})
@RestController
@RequestMapping("/category")
public class CateGoryController {
    
    @Resource
    private CategoryService categoryService;
    @RequireLogin
    @PostMapping("/add")
    public BaseResponse<Boolean> addCategory(@RequestBody CategoryAddRequest categoryAddRequest, HttpServletRequest request){
        //判空
        if(categoryAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddRequest, category);
        boolean categoryId = categoryService.save(category);
        return ResultUtils.success(categoryId);
    }

    @RequireLogin
    @DeleteMapping("/{id}")
    public BaseResponse<Boolean> deleteCategory(@PathVariable Long id) {
        if(id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = categoryService.removeById(id);
        return ResultUtils.success(res);
    }

    @RequireLogin
    @PostMapping("/update")
    public BaseResponse<Boolean> updateCategory(@RequestBody Category categoryUpdateRequest, HttpServletRequest request) {
        if(categoryUpdateRequest == null || categoryUpdateRequest.getCategoryId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = categoryService.updateById(categoryUpdateRequest);
        return ResultUtils.success(res);
    }

    @GetMapping("/get")
    public BaseResponse<Category> getCategoryById(long id) {
        if(id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Category category = categoryService.getById(id);
        return ResultUtils.success(category);
    }

    @GetMapping("/list")
    public BaseResponse<List<Category>> getCategoryList() {
        List<Category> categoryList;
        categoryList = categoryService.list();
        return ResultUtils.success(categoryList);
    }
    //
    @GetMapping("/list/page")
    public BaseResponse<Page> getCategoryListByPage(PageRequest categoryQueryRequest) {
        Page<Category> page = new Page(categoryQueryRequest.getPageNum(), categoryQueryRequest.getPageSize());
        Page<Category> page1 = categoryService.page(page);
        return ResultUtils.success(page1);
    }

}
